package com.ttdevs.es;

import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

import com.ttdevs.BaseActivity;

public class SettingsActivity extends BaseActivity implements OnCheckedChangeListener {
	
	private final static long[] pattern = { 10, 50 };
	private Vibrator vibrator;
	
	private ToggleButton tbHeadSet, tbUSB, tbSMS, tbLight, tbProximity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_settings);

		vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);// 震动
		initView();
	}

	private void initView() {
		tbHeadSet = ((ToggleButton) findViewById(R.id.tbHeadset));
		tbUSB = ((ToggleButton) findViewById(R.id.tbUSB));
		tbSMS = ((ToggleButton) findViewById(R.id.tbSMS));
		tbLight = ((ToggleButton) findViewById(R.id.tbLight));
		tbProximity = ((ToggleButton) findViewById(R.id.tbProximity));

		if (spUtil.getHeadSet()) {
			tbHeadSet.setChecked(true);
		}
		if (spUtil.getLight()) {
			tbLight.setChecked(true);
		}
		if (spUtil.getProximity()) {
			tbProximity.setChecked(true);
		}
		if (spUtil.getSMS()) {
			tbSMS.setChecked(true);
		}
		if (spUtil.getUSB()) {
			tbUSB.setChecked(true);
		}

		tbHeadSet.setOnCheckedChangeListener(this);
		tbUSB.setOnCheckedChangeListener(this);
		tbSMS.setOnCheckedChangeListener(this);
		tbLight.setOnCheckedChangeListener(this);
		tbProximity.setOnCheckedChangeListener(this);
	}

	// ***************************************************************
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		vibrator.vibrate(pattern, 1);
		switch (buttonView.getId()) {
		case R.id.tbHeadset:
			spUtil.putHeadSet(isChecked);
			break;
		case R.id.tbLight:
			spUtil.putLight(isChecked);
			break;
		case R.id.tbProximity:
			spUtil.putProximity(isChecked);
			break;
		case R.id.tbSMS:
			spUtil.putSMS(isChecked);
			break;
		case R.id.tbUSB:
			spUtil.putUSB(isChecked);
			break;

		default:
			break;
		}
	}
}